﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.IO;
    using System.Linq;
    using System.Windows;
    using System.Windows.Media;

    /// <summary>
    /// Interaction logic for SelectStyleWindow.xaml
    /// </summary>
    public partial class SelectStyleWindow : Window
        {

        private bool initialising = true;

        private IStyledServiceItem[] items = null;

        public SelectStyleWindow(params IStyledServiceItem[] itemsToAugment)
            {
            InitializeComponent();
            initialising = false;

            // Add items to list
            this.items = itemsToAugment;
            foreach (var style in State.Database.Styles.GetAllStyles().OrderBy(s => s.Name))
                {
                StylesListBox.Items.Add(style);
                }

            // Set current style
            if (itemsToAugment.Length > 0)
                {
                var item = itemsToAugment[0];
                StylesListBox.SelectedItem = item.Style;
                StylesListBox.ScrollIntoView(StylesListBox.SelectedItem);

                // Set override details
                if (item.StyleOverride != null)
                    {
                    if (item.StyleOverride.Colour != null)
                        {
                        OverrideColourRadioButton.IsChecked = true;
                        ColourControl.Brush = item.StyleOverride.Colour;
                        }
                    else if (item.StyleOverride.ImageFileName != null)
                        {
                        OverrideImageRadioButton.IsChecked = true;
                        ImageFileControl.FileName = item.StyleOverride.ImageFileName;
                        }
                    else
                        {
                        OverrideVideoRadioButton.IsChecked = true;
                        VideoFileControl.FileName = item.StyleOverride.VideoFileName;
                        }
                    }
                else OverrideNoneRadioButton.IsChecked = true;

                }

            // Update enabled state
            UpdateEnabledState();

            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;
            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            try
                {

                // Ensure a style has been selected
                if (StylesListBox.SelectedItems.Count == 0)
                    {
                    throw new Exception("Please select a style from the list.");
                    }

                // If overriding with an image, then an image file name must be specified
                if (OverrideImageRadioButton.IsChecked == true)
                    {
                    if (string.IsNullOrEmpty(ImageFileControl.FileName) || !File.Exists(ImageFileControl.FileName))
                        {
                        throw new Exception("You have chosen to change the background of the item to an image, but have not selected an image. Please choose an image file to use.");
                        }
                    }

                // If overriding with a video, then a file name must be specified
                if (OverrideVideoRadioButton.IsChecked == true)
                    {
                    if (string.IsNullOrEmpty(VideoFileControl.FileName) || !File.Exists(VideoFileControl.FileName))
                        {
                        throw new Exception("You have chosen to change the background of the item to a video, but have not selected a video. Please choose a video file to use.");
                        }
                    }

                // Apply changes
                foreach (var item in items)
                    {

                    // Apply the style
                    item.Style = (SongDatabase.Style)StylesListBox.SelectedItem;

                    // Apply the override settings
                    if (OverrideColourRadioButton.IsChecked == true)
                        {
                        item.StyleOverride = StyleOverride.CreateColour((SolidColorBrush)ColourControl.Brush);
                        }
                    else if (OverrideImageRadioButton.IsChecked == true)
                        {
                        item.StyleOverride = StyleOverride.CreateImage(ImageFileControl.FileName);
                        }
                    else if (OverrideVideoRadioButton.IsChecked == true)
                        {
                        item.StyleOverride = StyleOverride.CreateVideo(VideoFileControl.FileName);
                        }
                    else
                        {
                        item.StyleOverride = null;
                        }

                    }

                // Close the window
                this.DialogResult = true;

                }
            catch (Exception ex)
                {
                Utilities.ShowErrorMessage("Cannot Apply Style", ex.Message);
                }

            }

        private void RadioButtonChanged(object sender, RoutedEventArgs e)
            {
            UpdateEnabledState();
            }

        private void UpdateEnabledState()
            {
            if (!initialising)
                {
                var styleSelected = StylesListBox.SelectedItems.Count > 0;
                this.OverrideNoneRadioButton.IsEnabled = styleSelected;
                this.OverrideColourRadioButton.IsEnabled = styleSelected;
                this.OverrideImageRadioButton.IsEnabled = styleSelected;
                this.OverrideVideoRadioButton.IsEnabled = styleSelected;
                this.ColourControl.IsEnabled = styleSelected && OverrideColourRadioButton.IsChecked == true;
                this.ImageFileControl.IsEnabled = styleSelected && OverrideImageRadioButton.IsChecked == true;
                this.VideoFileControl.IsEnabled = styleSelected && OverrideVideoRadioButton.IsChecked == true;
                this.OKButton.IsEnabled = styleSelected;
                }
            }

        private void StylesListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
            {
            UpdateEnabledState();
            }

        private void StylesListBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
            this.OKButton_Click(null, null);
            }

        }

    }
